package com.qsxc.system.aspect;

import com.qsxc.system.annotation.DataSource;
import com.qsxc.system.config.datasource.DbContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * 通过AOP方式实现拦截和切换，根据Mapper路径来决定切换到哪个数据源
 *
 * @author 
 */
@Component
@Aspect
@Order(-100)
@Slf4j
public class DataSourceAspect {

	@Pointcut("@within(com.qsxc.system.annotation.DataSource) || @annotation(com.qsxc.system.annotation.DataSource)")
	public void pointCut() {
	}

	@Before("pointCut() && @annotation(dataSource)")
	public void doBefore(DataSource dataSource) {
//		log.info("选择数据源---" + dataSource.value().getValue());
		DbContextHolder.setDataSource(dataSource.value().getValue());
	}

	@After("pointCut()")
	public void doAfter() {
		DbContextHolder.clear();
	}
}
